<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/3/6
 * Time: 10:10
 */

namespace app\api\controller;


use think\Db;

class My extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 编辑信息
     * @return \think\response\Json
     * user_gender           性别 1 男  2 女
     * user_image            头像
     * user_username         昵称
     * user_sign             个性签名
     * user_birthday         生日
     * user_height           身高
     * user_education        学历
     * user_trade            行业
     * user_profession       职业
     * user_school           学校
     * user_interest         兴趣爱好
     * user_smoke            抽烟习惯
     * user_liquor           饮酒爱好
     * user_pet_type         宠物种类
     * user_pet_name         宠物名字
     * user_intro            自我介绍
     * user_sport            运动标签
     * user_weight           体重
     * user_xiong            胸围
     * user_yao              腰围
     * user_tun              臀围
     * sheng                 省
     * shi                   市
     * qu                    区
     */
    public function updateUserInfo(){
        $user_gender = input('user_gender'); // 性别   1 男   2 女
        $user_image = input('user_image'); // 头像
        $user_username = input('user_username'); // 昵称
        $user_sign = input('user_sign'); // 个性签名
        $user_birthday = input('user_birthday'); // 生日
        $user_height = input('user_height'); // 身高
        $user_education = input('user_education'); // 学历
        $user_trade = input('user_trade'); // 行业
        $user_profession = input('user_profession'); // 职业
        $user_school = input('user_school'); // 学校
        $user_interest = input('user_interest'); // 兴趣爱好
        $user_smoke = input('user_smoke'); // 抽烟习惯
        $user_liquor = input('user_liquor'); // 饮酒爱好
        $user_pet_type = input('user_pet_type'); // 宠物种类
        $user_pet_name = input('user_pet_name'); // 宠物名字
        $user_intro = input('user_intro'); // 自我介绍
        $user_sport = input('user_sport'); // 运动标签
        $user_weight = input('user_weight'); // 体重
        $user_xiong = input('user_xiong'); // 胸围
        $user_yao = input('user_yao'); // 腰围
        $user_tun = input('user_tun'); // 臀围
        $sheng = input('sheng');
        $shi = input('shi');
        $qu = input('qu');
        //--------------------------------
        $go_address = input('go_address');
        $choose_city = input('choose_city');
        $wait_other = input('wait_other');

        $update['user_update_time'] = $this->create_time;
        if(isset($user_gender) && $user_gender != '')
            $update['user_gender'] = $user_gender;
        //------------------------------------------------------------------------
        if(isset($go_address) && $go_address != '')
            $update['go_address'] = $go_address;
        if(isset($choose_city) && $choose_city != '')
            $update['choose_city'] = $choose_city;
        if(isset($wait_other) && $wait_other != '')
            $update['wait_other'] = $wait_other;
        //------------------------------------------------------------------------
        if(isset($user_image) && $user_image != '')
            $update['user_image'] = $user_image;
        if(isset($user_username) && $user_username != '')
            $update['user_username'] = $user_username;
        if(isset($user_sign) && $user_sign != '')
            $update['user_sign'] = $user_sign;
        if(isset($user_birthday) && $user_birthday != '')
            $update['user_birthday'] = $user_birthday;
            $update['user_const'] = $this->get_constellation($user_birthday);
        if(isset($user_height) && $user_height != '')
            $update['user_height'] = $user_height;
        if(isset($user_education) && $user_education != '')
            $update['user_education'] = $user_education;
        if(isset($user_trade) && $user_trade != '')
            $update['user_trade'] = $user_trade;
        if(isset($user_profession) && $user_profession != '')
            $update['user_profession'] = $user_profession;
        if(isset($user_school) && $user_school != '')
            $update['user_school'] = $user_school;
        if(isset($user_interest) && $user_interest != '')
            $update['user_interest'] = $user_interest;
        if(isset($user_smoke) && $user_smoke != '')
            $update['user_smoke'] = $user_smoke;
        if(isset($user_liquor) && $user_liquor != '')
            $update['user_liquor'] = $user_liquor;
        if(isset($user_pet_type) && $user_pet_type != '')
            $update['user_pet_type'] = $user_pet_type;
        if(isset($user_pet_name) && $user_pet_name != '')
            $update['user_pet_name'] = $user_pet_name;
        if(isset($user_intro) && $user_intro != '')
            $update['user_intro'] = $user_intro;
        if(isset($user_sport) && $user_sport != '')
            $update['user_sport'] = $user_sport;
        if(isset($user_weight) && $user_weight != '')
            $update['user_weight'] = $user_weight;
        if(isset($user_xiong) && $user_xiong != '')
            $update['user_xiong'] = $user_xiong;
        if(isset($user_yao) && $user_yao != '')
            $update['user_yao'] = $user_yao;
        if(isset($user_tun) && $user_tun != '')
            $update['user_tun'] = $user_tun;
        if(isset($sheng) && $sheng != '' && isset($shi) && $shi != '' && isset($qu) && $qu != '')
            $update['user_sheng'] = $sheng;
            $update['user_shi'] = $shi;
            $update['user_qu'] = $qu;
        
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        $list = Db::table('up_u_user')->where('user_id', $this->user_id)->find();
        if($res){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 用户信息
     * @return \think\response\Json
     * other_user_id   他人ID
     */
    public function userInfo(){
        $ohter_user_id = input('other_user_id');
        if(isset($ohter_user_id)){
            $uid = $ohter_user_id;
        }else{
            $uid = $this->user_id;
        }
        $info = Db::table('up_u_user')
            ->join('up_area', 'district_id = user_shi', 'left')
            ->field('user_status,user_username, user_image, user_uid, user_vip_level, user_birthday, district, 
            user_const, user_gender, user_balance, user_trade, user_profession, user_school, 
            user_education, user_height, user_smoke, user_liquor, user_interest, user_pet_name, user_sport, user_intro, 
            user_weight, user_xiong, user_yao, user_tun, user_online, user_last_online_time, user_last_unline_time, 
            user_sign, user_pet_type, go_address, choose_city, wait_other')
            ->where('user_id', $uid)
            ->find();
        // 年龄
        $info['age'] = $this->getAge($info['user_birthday']);
        // vip等级
        if($info['user_gender'] == 1){
            $info['vip'] = $this->getVip($uid);
        }else{
            $info['vip'] = 1;
        }
        // 去过的国家
//        $go_address = Db::table('up_go_address')
//            ->where('go_user_id', $uid)
//            ->column('go_address_name');
//        if($go_address){
//            $info['go_address'] = implode('  ', $go_address);
//        }else{
//           $info['go_address'] = '';
//        }
        
        // 他人动态
        if(isset($ohter_user_id)){
            // 动态
            $DP = Db::table('up_wall')
                ->field('wall_id, wall_dz, wall_content, wall_img, wall_video, wall_add_time, wall_type')
                ->where('wall_user_id', $uid)
                ->order('wall_add_time desc')
                ->limit(1)
                ->find();
            if($DP){
                // 是否点赞
                $dz = Db::table('up_dp_dz')
                    ->where('dz_user_id', $this->user_id)
                    ->where('dz_by_dp_id', $DP['wall_id'])
                    ->find();
                if($dz){
                    $DP['dz'] = 1;
                }else{
                    $DP['dz'] = 2;
                }
                $DP['dz_gold'] = $DP['wall_dz'] * 30;  // 点赞金币
                $DP['add_time'] = $this->formatTime(strtotime($DP['wall_add_time'])); // 发布时间
                // 图片
                if($DP['wall_type'] == 1){
                    $wall_img = explode(',', trim($DP['wall_img'], ','));
                    $DP['wall_img'] = $wall_img;
                }
                $info['dp'] = $DP;
            }else{
                $info['dp'] = [];
            }
            // 是否是好友
            $is_friend = Db::table('up_friend')
                ->where('friend_status', 2)
                ->where(function ($query) use ($uid) {
                    $query->where('user_id', $uid)
                        ->whereOr('other_user_id', $uid);
                })
                ->find();
            if($is_friend){
                $info['friend'] = 1; // 是好友
            }else{
                $info['friend'] = 2; // 不是好友
            }
            // 最后下线时间
            $info['last_unline_time'] = $this->formatTime(strtotime($info['user_last_unline_time']));
        }
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 用户状态设置
     * @return \think\response\Json
     * set_obj     操作对象   1 在线状态   2 地理位置    3 vip等级
     */
    public function isShowStatus(){
        $set_obj = input('set_obj'); // 操作对象   1 在线状态   2 地理位置   3 vip等级
        $status = Db::table('up_u_user')
            ->field('user_online_is_show, user_addr_is_show, user_vip_is_show')
            ->where('user_id', $this->user_id)
            ->find();
        $field = '';
        $is_show = 1;
        if($set_obj == 1){
            $field = 'user_online_is_show';
            if($status['user_online_is_show'] == 1){
                $is_show = 2;
            }
        }
        if($set_obj == 2){
            $field = 'user_addr_is_show';
            if($status['user_addr_is_show'] == 1){
                $is_show = 2;
            }
        }
        if($set_obj == 3){
            $field = 'user_vip_is_show';
            if($status['user_vip_is_show'] == 1){
                $is_show = 2;
            }
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField($field, $is_show);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 用户地理位置、在线状态、vip等级显示状态
     * @return \think\response\Json
     */
    public function showStatus(){
        $show = Db::table('up_u_user')
            ->field('user_online_is_show, user_addr_is_show, user_vip_is_show')
            ->where('user_id', $this->user_id)
            ->find();
        return json(['status' => 200, 'msg' => $show]);
    }
    
    /**
     * 客服信息
     * @return \think\response\Json
     */
    public function serviceInfo(){
        $phone = Db::table('up_other')
            ->field('other_phone, other_qrcode_link')
            ->where('other_id', 2)
            ->find();
        return json(['status' => 200, 'msg' => $phone]);
    }
    
    /**
     * 用户协议
     * @return \think\response\Json
     */
    public function userAgreement(){
        $agreement = Db::table('up_other')->where('other_id', 3)->value('other_user_agreement');
        return json(['status' => 200, 'msg' => $agreement]);
    }
    
    /**
     * 隐私协议
     * @return \think\response\Json
     */
    public function ysAgreement(){
        $agreement = Db::table('up_other')->where('other_id', 4)->value('other_ys_agreement');
        return json(['status' => 200, 'msg' => $agreement]);
    }
    
    /**
     * 注销账号
     * @return \think\response\Json
     */
    public function logout(){
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_status', 3);
        if($res){
            return json(['status' => 200, 'msg' => '注销成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，注销失败']);
        }
    }
    
    /**
     * 登录/登出
     * @return \think\response\Json
     * login_type    1 登录   2 登出
     */
    public function userLoginStatus(){
        $login_type = input('login_type');
        if($login_type == 1){ // 上线
            $data = [
                'user_last_online_time' => $this->create_time,
                'user_online' => 1
            ];
        }else{
            $data = [
                'user_last_unline_time' => $this->create_time,
                'user_online' => 2
            ];
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($data);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作成功']);
        }
    }
    
    /**
     * 消息列表
     * @return \think\response\Json
     */
    public function userNotice(){
        $list = $this->notice(1);
        if($list){
            foreach($list as $k => $v){
                if(strpos($v['notice_read_user_id'], ',' . $this->user_id . ',') !== false){
                    $list[$k]['is_read'] = 1; // 已读
                }else{
                    $list[$k]['is_read'] = 2; // 未读
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 消息详情
     * @return \think\response\Json
     * notice_id   消息ID
     */
    public function noticeDetail(){
        $notice_id = input('notice_id');
        $info = Db::table('up_notice')
            ->field('notice_title, notice_content, notice_read_user_id, notice_time')
            ->where('notice_id', $notice_id)
            ->find();
        if(strpos($info['notice_read_user_id'], ',' . $this->user_id . ',') === false){
            $read_user = $info['notice_read_user_id'] . $this->user_id . ',';
            Db::table('up_notice')
                ->where('notice_id', $notice_id)
                ->setField('notice_read_user_id', $read_user);
        }
        return json(['status' => 200, 'msg' => $info]);
    }
    
    /**
     * 未读消息个数
     * @return \think\response\Json
     */
    public function unreadNotice(){
        $notice = $this->notice(2);
        $num = count($notice);
        return json(['status' => 200, 'msg' => $num]);
    }
    

    
    /**
     * 添加银行卡
     * @return \think\response\Json
     * bank_code     卡号
     * bank_name     银行
     * bank_user_real_name    真实姓名
     */
    public function addBank(){
        $bank_code = input('bank_code');
        $bank_name = input('bank_name');
        $bank_user_real_name = input('bank_user_real_name');
        $is_have = Db::table('up_bank')->where('bank_user_id', $this->user_id)->column('bank_code');
        if(in_array($bank_code, $is_have)){
            return json(['status' => 0, 'msg' => '请不要重复添加']);
        }
        if(count($is_have) == 0){
            $default = 1;
        }else{
            if(count($is_have) == 4){
                return json(['statua' => 0, 'msg' => '最多只能添加4张银行卡']);
            }
            $default = 2;
        }
        $data = [
            'bank_code' => $bank_code,
            'bank_name' => $bank_name,
            'bank_user_real_name' => $bank_user_real_name,
            'bank_time' => $this->create_time,
            'bank_user_id' => $this->user_id,
            'bank_is_default' => $default
        ];
        $res = Db::table('up_bank')->insert($data);
        if($res){
            return json(['status' => 200, 'msg' => '添加成功']);
        }else{
            return json(['stauts' => 0, 'msg' => '添加失败']);
        }
    }
    
    /**
     * 银行卡列表
     * @return \think\response\Json
     */
    public function bankList(){
        $list = Db::table('up_bank')
            ->where('bank_user_id', $this->user_id)
            ->field('bank_id, bank_code, bank_name, bank_user_real_name')
            ->order('bank_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除银行卡
     * @return \think\response\Json
     * bank_id    ID
     */
    public function delBank(){
        $bank_id = input('bank_id');
        $default = Db::table('up_bank')
            ->field('bank_is_default')
            ->where('bank_id', $bank_id)
            ->find();
        Db::startTrans();
        try{
            if($default['bank_is_default'] == 1){ // 如果删除的是默认银行卡
                $bank = Db::table('up_bank')
                    ->where('bank_user_id', $this->user_id)
                    ->column('bank_id');
                if(count($bank) > 1){ // 如果有多张银行卡，则将第二张改为默认，删除第一张
                    Db::table('up_bank')->delete($bank_id);
                    Db::table('up_bank')->where('bank_id', $bank[1])->setField('bank_is_default', 1);
                }else{
                    Db::table('up_bank')->delete($bank_id);
                }
            }else{
                Db::table('up_bank')->delete($bank_id);
            }
            Db::commit();
            return json(['status' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 提现规则
     * @return \think\response\Json
     */
    public function txRule(){
        $rule = Db::table('up_other')->where('other_id', 5)->value('other_tx_rule');
        return json(['status' => 200, 'msg' => $rule]);
    }
    

    
    /**
     * 默认银行卡信息
     * @return \think\response\Json
     */
    public function bankInfo(){
        // 银行卡
        $bank = Db::table('up_bank')
            ->where('bank_user_id', $this->user_id)
            ->find();
        if(!$bank){
            return json(['status' => 0, 'msg' => '请先添加银行卡']);
        }else{
            $default = Db::table('up_bank')
                ->where('bank_user_id', $this->user_id)
                ->where('bank_is_default', 1)
                ->field('bank_code, bank_name')
                ->find();
            if($default){
                return json(['status' => 200, 'msg' => $default]);
            }else{
                return json(['status' => 200, 'msg' => '请先设置默认银行卡']);
            }
        }
    }
    
    /**
     * 提现申请
     * @return \think\response\Json
     * withdraw_money     申请提现的RMB
     * withdraw_gold      申请提现的金币
     */
    public function tiXian(){
        $withdraw_money = input('withdraw_money');
        $withdraw_gold = input('withdraw_gold');
        $user = Db::table('up_u_user')->field('user_balance')->where('user_id', $this->user_id)->find();
        if($user['user_balance'] < $withdraw_gold){
            return json(['status' => 0, 'msg' => '余额不足']);
        }
        $bank = Db::table('up_bank')
            ->where('bank_user_id', $this->user_id)
            ->where('bank_is_default', 1)
            ->field('bank_code, bank_name, bank_user_real_name')
            ->find();
        $insert = [
            'withdraw_user_id' => $this->user_id,
            'withdraw_money' => $withdraw_money,
            'withdraw_gold' => $withdraw_gold,
            'withdraw_bank_code' => $bank['bank_code'],
            'withdraw_bank_name' => $bank['bank_name'],
            'withdraw_user_real_name' => $bank['bank_user_real_name'],
            'withdraw_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            Db::table('up_withdraw')->insert($insert);
            Db::table('up_u_user')->where('user_id', $this->user_id)->setDec('user_balance', $withdraw_gold);
            Db::commit();
            return json(['statua' => 200, 'msg' => '申请成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '申请失败']);
        }
    }
    
    /**
     * 发布任务
     * @return \think\response\Json
     * task_type     任务类型   1 素颜照  2 自我介绍视频  3 笑话
     * task_content  任务内容
     * task_gold     金币数
     */
    public function publishTask(){
        $task_type = input('task_type'); // 1 素颜照   2 自我介绍视频   3 笑话
        $task_content = input('task_content');
        $task_gold = input('task_gold');
        $user_balance = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_balance')
            ->find();
        if($user_balance['user_balance'] < $task_gold){
            return json(['status' => 0, 'msg' => '余额不足']);
        }
        $insert = [
            'task_user_id' => $this->user_id,
            'task_content' => $task_content,
            'task_type' => $task_type,
            'task_gold' => $task_gold,
            'task_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            // 插入任务表
            Db::table('up_task')->insert($insert);
            // 扣除金币
            Db::table('up_u_user')->where('user_id', $this->user_id)->setDec('user_balance', $task_gold);
            // 消耗总金币数
            Db::table('up_u_user')->where('user_id', $this->user_id)->setInc('user_consume', $task_gold);
            Db::commit();
            return json(['status' => 200, 'msg' => '发布成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 邀请好友
     * @return \think\response\Json
     */
    public function invite(){
        $phone = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_phone');
        $file = Qrcode($phone);
        if($file){
            return json(['status' => 200, 'msg' => $file]);
        }else{
            return json(['status' => 0, 'msg' => '分享失败']);
        }
    }
    
    /**
     * 点赞
     * @return \think\response\Json
     * other_user_id     被点赞用户ID
     * dp_id             被点赞的动态ID
     */
    public function dpdz(){
        $other_user_id = input('other_user_id');
        $dp_id = input('dp_id');
        $user = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_balance')
            ->find();
        if($user['user_balance'] < 30){
            return json(['status' => 0, 'msg' => '余额不足']);
        }
        $insert = [
            'dz_user_id' => $this->user_id,
            'dz_by_user_id' => $other_user_id,
            'dz_by_dp_id' => $dp_id,
            'dz_gold' => 30,
            'dz_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            // 插入点赞表
            Db::table('up_dp_dz')->insert($insert);
            // 扣自己的余额
            Db::table('up_u_user')->where('user_id', $this->user_id)->setDec('user_balance', 30);
            // 消费的总金币数
            Db::table('up_u_user')->where('user_id', $this->user_id)->setInc('user_consume', 30);
            // 给对方加余额
            Db::table('up_u_user')->where('user_id', $other_user_id)->setInc('user_balance', 30);
            // 点赞数量加1
            Db::table('up_wall')->where('wall_id', $dp_id)->setInc('wall_dz');
            // 动态消息表
            $this->insertDpNotice(3, $dp_id, $other_user_id);
            Db::commit();
            return json(['status' => 200, 'msg' => '点赞成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '点赞失败']);
        }
    }
    
    /**
     * 我对这条动态的点赞数量
     * @return \think\response\Json
     * dp_id    动态ID
     */
    public function dzNum(){
        $dp_id = input('dp_id');
        $num = Db::table('up_dp_dz')
            ->where('dz_user_id', $this->user_id)
            ->where('dz_by_dp_id', $dp_id)
            ->count();
        return json(['status' => 200, 'msg' => $num]);
    }
    
    /**
     * 动态评论
     * @return \think\response\Json
     * dp_id       被评论的动态ID
     * comment_content     评论内容
     * other_user_id       被评论用户的ID
     */
    public function dpComment(){
        $dp_id = input('dp_id');
        $comment_content = input('comment_content');
        $other_user_id = input('other_user_id');
        $insert = [
            'comment_content' => $comment_content,
            'comment_user_id' => $this->user_id,
            'comment_by_user_id' => $other_user_id,
            'comment_dp_id' => $dp_id,
            'comment_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            // 评论表
            Db::table('up_dp_comment')->insert($insert);
            // 动态消息表
            $this->insertDpNotice(1, $dp_id, $other_user_id, $comment_content);
            Db::commit();
            return json(['status' => 200, 'msg' => '评论成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '评论失败']);
        }
    }
    
    /**
     * 动态更新用户省市区
     * @return \think\response\Json
     *
     */
    public function updateArea(){
        $sheng = input('user_sheng');
        $shi = input('user_shi');
        $qu = input('user_qu');
        $area = [
            'user_sheng' => $sheng,
            'user_shi' => $shi,
            'user_qu' => $qu
        ];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($area);
        if($res){
            return json(['status' => 200, 'msg' => '更新成功']);
        }else{
            return json(['status' => 0, 'msg' => '更新失败']);
        }
    }
    
    /**
     * 发布动态
     * @return \think\response\Json
     * wall_lon       经度
     * wall_lat       纬度
     * wall_content   内容
     * wall_img       图片
     * wall_video     视频
     * wall_sheng     省
     * wall_shi       市
     * wall_qu        区
     */
    public function addDp(){

        $wall_lon = input('wall_lon');
        $wall_lat = input('wall_lat');
        $wall_content = input('wall_content');
        $wall_img = input('wall_img');
        $wall_video = input('wall_video');
        $wall_sheng = input('wall_sheng');
        $wall_shi = input('wall_shi');
        $wall_qu = input('wall_qu');
        //todo 新增图片视频类型  1图片 2视频
        $wall_type = input('param.wall_type');
        $wall = [
            'wall_user_id' => $this->user_id,
            'wall_lon' => $wall_lon,
            'wall_lat' => $wall_lat,
            'wall_content' => $wall_content,
            'wall_img' => $wall_img,
            'wall_video' => $wall_video,
            'wall_add_time' => $this->create_time,
            'wall_sheng' => $wall_sheng,
            'wall_shi' => $wall_shi,
            'wall_qu' => $wall_qu,
            'wall_type'=>$wall_type
        ];
        $res = Db::table('up_wall')->insert($wall);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 我的邀请
     * @return \think\response\Json
     */
    public function myInvite(){
        $phone = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_phone');
        $invite = Db::table('up_u_user')
            ->where('user_invite_phone', $phone)
            ->field('user_id, user_username, user_image')
            ->order('user_time desc')
            ->select();
        if($invite){
            return json(['status' => 200, 'msg' => $invite]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 提现/充值比例
     * @return \think\response\Json
     * type      类型   1 充值   2 提现
     */
    public function prop(){
        $type = input('type');
        if($type == 1){
            $where['other_id'] = 7;
            $field = 'other_recharge_prop';
        }else{
            $where['other_id'] = 6;
            $field = 'other_tx_prop';
        }
        $prop = Db::table('up_other')->where($where)->value($field);
        return json(['status' => 200, 'msg' => $prop]);
    }
    
    /**
     * 世界墙列表
     * @return \think\response\Json
     */
    public function wallList(){
        $list = Db::table('up_wall')
            ->join('up_u_user', 'wall_user_id = user_id', 'left')
            ->field('wall_id,wall_sheng, user_username, user_image, user_vip_level, user_birthday, user_const, 
            wall_dz, wall_content, wall_img, wall_video, wall_add_time, wall_user_id, wall_id')
            ->page($this->page, 10)
            ->order('wall_add_time desc')
            ->select();
        if($list){
            foreach($list as $k => $v){
                // 是否点赞
                $dz = Db::table('up_dp_dz')
                    ->where('dz_user_id', $this->user_id)
                    ->where('dz_by_dp_id', $v['wall_id'])
                    ->find();
                if($dz){
                    $list[$k]['dz'] = 1;
                }else{
                    $list[$k]['dz'] = 2;
                }
                // 时间
                $list[$k]['wall_add_time'] = $this->formatTime(strtotime($v['wall_add_time']));
                // 年龄
                $list[$k]['user_age'] = $this->getAge($v['user_birthday']);
                // 评论
                $where['comment_dp_id'] = $v['wall_id'];
                if($v['wall_user_id'] != $this->user_id){ // 不是我发的动态，我只能看到我发的评论
                    $where['comment_user_id'] = $this->user_id;
                }
                $comment = Db::table('up_dp_comment')
                    ->join('up_u_user', 'user_id = comment_user_id', 'left')
                    ->where($where)
                    ->field('comment_id, comment_content, comment_user_id, comment_time, user_username, 
                    user_image, comment_dp_id')
                    ->order('comment_time desc')
                    ->select();
                if($comment){
                    foreach($comment as $kk => $vv){
                        $repaly = Db::table('up_dp_comment_repaly')
                            ->join('up_u_user', 'repaly_user_id = user_id', 'left')
                            ->where('repaly_comment_id', $vv['comment_id'])
                            ->field('user_username, user_image, repaly_content, repaly_time')
                            ->order('repaly_time desc')
                            ->select();
                        if($repaly){
                            $comment[$kk]['repaly'] = $repaly;
                        }else{
                            $comment[$kk]['repaly'] = [];
                        }
                    }
                    $list[$k]['comment'] = $comment;
                }else{
                    $list[$k]['comment'] = [];
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 回复评论
     * @return \think\response\Json
     * repaly_comment_id   回复的评论ID
     * repaly_content   回复内容
     */
    public function commentRepaly(){
        $repaly_comment_id = input('repaly_comment_id');
        $repaly_content = trim(input('repaly_content'), ' ');
        $comment_info = Db::table('up_dp_comment')
            ->field('comment_user_id, comment_dp_id')
            ->where('comment_id', $repaly_comment_id)
            ->find();
        $insert = [
            'repaly_dp_id' => $comment_info['comment_dp_id'],
            'repaly_comment_id' =>$repaly_comment_id,
            'repaly_user_id' => $this->user_id,
            'repaly_content' => $repaly_content,
            'repaly_time' => $this->create_time
        ];
        Db::startTrans();
        try{
            // 回复
            Db::table('up_dp_comment_repaly')->insert($insert);
            // 动态消息
            $this->insertDpNotice(2, $repaly_comment_id, $comment_info['comment_user_id'], $repaly_content);
            Db::commit();
            return json(['status' => 200, 'msg' => '回复成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '发布失败']);
        }
    }
    
    /**
     * 用户动态消息列表
     * @return \think\response\Json
     * page   页码
     */
    public function noticeList(){
        $list = $this->dpNotice(1);
        if($list){
            foreach($list as $k => $v){
                $list[$k]['dn_time'] = $this->formatTime(strtotime($v['dn_time']));
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 未读动态消息数量
     * @return \think\response\Json
     */
    public function unread_num_dp(){
        $num = count($this->dpNotice(2));
        return json(['status' => 200, 'msg' => $num]);
    }
    
    /**
     * 更新动态消息为已读
     * @return \think\response\Json
     * dn_id     动态消息ID
     */
    public function changeNoticeStatus(){
        $dn_id = input('dn_id');
        $res = Db::table('up_dp_notice')->where('dn_id', $dn_id)->setField('dn_is_readed', 2);
        if($res){
            return json(['status' => 200, 'msg' => '更新成功']);
        }else{
            return json(['status' => 0, 'msg' => '更新失败']);
        }
    }
    
    /**
     * 举报
     * @return \think\response\Json
     * other_user_id    被举报的人
     */
    public function report(){
        $other_user_id = input('other_user_id');
        $time = date('Y-m-d H:i:s', strtotime('-24 hours'));
        $is_report = Db::table('up_report')
            ->where('report_user_id', $this->user_id)
            ->where('report_other_user_id', $other_user_id)
            ->whereTime('report_time', '>', $time)
            ->find();
        if($is_report){
            return json(['status' => 0, 'msg' => '24小时内不可重复举报同一个用户']);
        }
        $insert = [
            'report_user_id' => $this->user_id,
            'report_other_user_id' => $other_user_id,
            'report_time' => $this->create_time
        ];
        $res = Db::table('up_report')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '举报成功']);
        }else{
            return json(['status' => 0, 'msg' => '举报失败']);
        }
    }
    
    /**
     * 消息
     * @param $range_type     1 列表    2 未读
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function notice($range_type){
        $where['notice_receive_user_id'] = [['like', '%,' . $this->user_id . ',%'], ['=', -1], 'or'];
        $data = Db::table('up_notice')
            ->field('notice_id, notice_title, notice_read_user_id, notice_time, notice_type, notice_party_id')
            ->where('notice_del_user_id', 'notlike', '%,' . $this->user_id . ',%')
            ->where($where);
        if($range_type == 2){ // 未读
            $data->where('notice_read_user_id', 'notlike', '%,' . $this->user_id . ',%');
        }
        if($range_type == 1){ // 列表
            $data->order('notice_time desc')->page($this->page, 10);
        }
        $data = $data->select();
        return $data;
    }
    
    /**
     * 动态消息
     * @param $range_type    1 列表   2 未读数量
     * @return $this|false|\PDOStatement|string|\think\Collection
     */
    private function dpNotice($range_type){
        $data = Db::table('up_dp_notice')
            ->where('dn_by_user_id', $this->user_id);
        if($range_type == 1){
            $data->join('up_u_user', 'user_id = dn_user_id', 'left')
                ->field('dn_id, user_username, dn_type, dn_object_id, dn_content, dn_is_readed, 
                dn_time')
                ->order('dn_time desc')
                ->page($this->page, 10);
        }
        if($range_type == 2){
            $data->where('dn_is_readed', 1);
        }
        $data = $data->select();
        return $data;
    }
    
    /**
     * 动态消息表
     * @param $notice_type    1 评论动态   2 回复评论   3 点赞动态
     * @param $object_id      动态/评论ID
     * @param $by_user_id     被评论动态/被点赞动态/被回复评论的发布者
     * @param string $dn_content      评论/回复内容
     */
    private function insertDpNotice($notice_type, $object_id, $by_user_id, $dn_content = ''){
        $insert = [
            'dn_user_id' => $this->user_id,
            'dn_type' => $notice_type,
            'dn_object_id' => $object_id,
            'dn_by_user_id' => $by_user_id,
            'dn_content' => $dn_content,
            'dn_time' => $this->create_time
        ];
        Db::table('up_dp_notice')->insert($insert);
    }

    /**
     * 我的账户
     * @return \think\response\Json
     */
    public function myAccount(){
        // 余额&送出的礼物金额
        $gold = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->field('user_balance, user_gift_gold')
            ->find();
        // 发出的红包金额
        $redbag = Db::table('up_envelopes')->where('user_id', $this->user_id)->sum('envelopes_price');
        $surplus = Db::table('up_envelopes')->where('user_id', $this->user_id)->sum('surplus_price');
        $redbag_price = $redbag - $surplus;
        $account = [
            'balance' => $gold['user_balance'],
            'redbag_price' => $redbag_price,
            'gift' => $gold['user_gift_gold']
        ];
        return json(['status' => 200, 'msg' => $account]);
    }

    /**
     * 充值记录
     * @return \think\response\Json
     */
    public function rechargeLog(){
        $list = Db::table('up_recharge')
            ->join('up_u_user', 'user_id = recharge_user_id', 'left')
            ->where('recharge_user_id', $this->user_id)
            ->where('recharge_status', 2)
            ->field('recharge_id, recharge_money, recharge_gold, recharge_num, recharge_type, recharge_time, 
            user_balance')
            ->order('recharge_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    /**
     * 用户余额/礼券
     * @return \think\response\Json
     */
    public function userBalance(){
        $balance = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_balance');
        return json(['status' => 200, 'msg' => $balance]);
    }
    public function otherRechargeProp(){
        $other_recharge_prop = Db::table('up_other')->where('other_id',7)->value('other_recharge_prop');
        return json(['status' => 200, 'msg' => $other_recharge_prop]);

    }
}